﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using HslCommunication;
using HslCommunication.ModBus;
using WindowsFormsApp2.PLC;
using WindowsFormsApp2.PLCmodbus;
using YJYpublicClass;

namespace WindowsFormsApp2.ModbusRtuSet
{
    public class ModbusRtuClient
    {
        private ModbusRtu rtuClient = new ModbusRtu();

        public Boolean IsConnect = false;
        public string rtuSavePath = "ModbusRtu.xml";

        private static object lockobj = new object();
        public string Mes
        {
            get;
            set;
        }
        public ModbusRtuParams rtuConfig { get; set; } = new ModbusRtuParams();

        public Boolean Connect()
        {
            try
            {
                if (rtuClient.IsOpen())
                {
                    return true;
                }

                rtuClient.SerialPortInni(sp =>
                {
                    sp.PortName = rtuConfig.Com;
                    sp.BaudRate = rtuConfig.BaudRate;
                    sp.DataBits = rtuConfig.DataBits;
                    sp.StopBits = rtuConfig._StopBits;
                    sp.Parity = rtuConfig._Parity;
                });
                rtuClient.Open(); // 打开
                IsConnect = rtuClient.IsOpen();

                return IsConnect;
            }
            catch (Exception ex)
            {

                MessageBox.Show("rtu串口打开失败");
                return false;
            }
        }


        public Boolean Init()
        {
            LoadConfig();
            if (Connect())
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public void Close()
        {
            rtuClient.Close();
        }


        public bool isConnect()
        {
            return IsConnect;
        }


        public void ReadShort(string address, out short value)
        {
            short result = 0;
            try
            {

                if (IsConnect)
                {
                    HslCommunication.OperateResult<short> t = rtuClient.ReadInt16(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content;
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }

        }

        public Boolean WriteShort(string address, short value)
        {
            try
            {
                if (IsConnect)
                {
                    if (rtuClient.Write(address, value).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public void ReadDouble(string address, out double value)
        {
            double result = 0;
            try
            {

                if (IsConnect)
                {
                    HslCommunication.OperateResult<double> t = rtuClient.ReadDouble(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content;
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }
        }


        public Boolean WriteDouble(string address, double value)
        {
            try
            {
                if (IsConnect)
                {
                    if (rtuClient.Write(address, value).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public void ReadCoil(string address, out bool value)
        {
            bool result = false;
            try
            {

                if (IsConnect)
                {
                    HslCommunication.OperateResult<bool> t = rtuClient.ReadCoil(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content;
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }
        }

        public Boolean WriteCoil(string address, bool value)
        {
            try
            {
                if (IsConnect)
                {
                    if (rtuClient.Write(address, value).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public ResultFunc Saveconfig()
        {
            try
            {
                if (XmlHelper.SaveXML(rtuSavePath, rtuConfig) == "")
                {
                    return new ResultFunc(Funstate.OK, "");
                }
                else
                {
                    return new ResultFunc(Funstate.异常, "保存失败");
                }
            }
            catch (Exception ex)
            {

                return new ResultFunc(Funstate.异常, ex.ToString());
            }

        }

        public void LoadConfig()
        {
            if (!File.Exists(rtuSavePath))
            {
                rtuConfig = new ModbusRtuParams();
            }
            else
            {
                rtuConfig = XmlHelper.LoadXML<ModbusRtuParams>(rtuSavePath);
            }
        }

        public void setConfig()
        {
            //Frm_Rtu tempform = new Frm_Rtu(this);
            //tempform.Show();
        }
    }
}
